<?php 
/**
* admin 学校管理，以及学校管理员
*/
class Admin_school extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if(!$this->session->userdata('r_user_in'))
            redirect('home');
        //权限检测
        if($this->session->userdata('r_role_id') != 1)
            redirect('home');
    }
    function index()
    {
        $data['title']='学校管理';
        $this->load->model('school_model');
        $this->load->model('school_admin_model');

        $schools = $this->school_model->get_all_order();
        $data['schools'] = $schools;
        $this->load->view('admin_school/index',$data);
    }
    function add()
    {
        //表单验证
        if($this->form_validation->run('admin_school/add'))
        {
            //post方法
            //读取数据
            $name=$this->input->post('name');

            //将模块写入数据库
            $item = array(
                'name' => $name
                 );
            
            $this->load->model('school_model');
            $this->load->model('major_model');
            if($this->school_model->add($item))
            {
                $school_id=$this->db->insert_id();
                //新建一个专业
                $major = array(
                    'name' => '电工学',
                    'school_id' => $school_id
                     );
                //检查如果这个学校的专业名称是否已经存在
                if($this->major_model->check_major_name_exist_in_a_school($major_name,$school_id))
                {
                    $this->session->set_flashdata('message','操作成功');
                    redirect('admin_school/index');
                }
                //不存在则自动添加专业
                else if($this->major_model->add($major)){
                    $this->session->set_flashdata('message','操作成功');
                    redirect('admin_school/index');
                }
                //不成功则删掉添加的学校并返回信息
                else{
                    $this->school_model->delete($school_id);
                    $this->session->set_flashdata('message','自动添加专业操作失败，学校已删除');
                    redirect('admin_school/add');
                }                       
            }
            $this->session->set_flashdata('message','操作失败');
            redirect('admin_school/add');
        }
        //get方法返回add页面
        $data['title']='添加学校';
        $this->load->view('admin_school/add',$data);
    }
    /**
    * 异步jquery验证
    * @return [type] [description]
    */
    function check_school_name_exist()
    {
        $name=$this->input->post('name');
        $this->load->model('school_model');
        if($this->school_model->school_name_exist($name))
            echo json_encode(FALSE);
        else 
            echo json_encode(TRUE);
    }
    /**
    * CI的表单回调函数验证
    * @param  [type] $str [description]
    * @return [type]      [description]
    */
    function school_name_check($name)
    {
        $this->load->model('school_model');
        if($this->school_model->school_name_exist($name))
            return FALSE;
        else 
            return TRUE;
    }
    function edit($id = 0)
    {
        if($id)
        {
            //get方法
            $this->load->model('school_model');
            $school=$this->school_model->get_by_id($id);
            if(!$school)
                return $this->load->view('error');
            $data['title']='编辑学校信息';
            $data['school']=$school;

            
            $this->load->view('admin_school/edit',$data);
        }
        else
        {
            $this->load->model('school_model');
            $id=$this->input->post('id');
            $school=$this->school_model->get_by_id($id);
            if(!$school)
                return $this->load->view('error');
            if($this->form_validation->run('admin_school/edit'))
            {
                //post方法
                $name=$this->input->post('name');

                //将内容写入数据库
                $item = array(
                    'name' => $name
                     );
                
                //数据库操作
                if($this->school_model->update($id,$item))
                {
                    $this->session->set_flashdata('message','操作成功');
                    redirect('admin_school/index');   
                }
                else
                {
                    $this->session->set_flashdata('message','操作失败');
                    redirect('admin_school/index');   
                }
            }
        }
    }
    function delete($id)
    {
        $this->load->model('school_model');
        $this->load->model('school_admin_model');
        $this->load->model('student_model');
        $this->load->model('major_model');
        $this->load->model('logic_class_model');
        $this->load->model('teacher_model');
        $this->load->model('user_model');
        if(!$this->school_model->get_by_id($id))
            return $this->load->view('error');
        $students = $this->student_model->get_by_school_id($id);
        $logic_classes = $this->logic_class_model->get_by_school_id($id);
        $teachers = $this->teacher_model->get_by_school_id($id);
        if($students||$logic_classes||$teachers)
        {
            $this->session->set_flashdata('message','存在相关引用，操作失败');
            redirect('admin_school/index');
        }
        //删除该学校的管理员
        $school_admins = $this->school_admin_model->get_by_school_id($id);
        foreach ($school_admins as $item) {
            $this->school_admin_model->delete($item['id']);
            $this->user_model->delete($item['user_id']);
        }
        $majors = $this->major_model->get_by_school_id($id);
        //删除学校的专业
        foreach ($majors as $major) {
            $this->major_model->delete($major['id']);
        }
        $majors = $this->major_model->get_by_school_id($id);
        if(!$majors&&$this->school_model->delete($id)){
            $this->session->set_flashdata('message','操作成功');
        }
        else
            $this->session->set_flashdata('message','存在相关引用，操作失败');

        redirect('admin_school/index');
    }
    function check($id)
    {
        $data['title']='查看学校';
        $this->load->model('school_model');
        $this->load->model('school_admin_model');
        $this->load->model('major_model');
        $this->load->model('teacher_model');
        $this->load->model('logic_class_model');
        $this->load->model('student_model');
        $school=$this->school_model->get_by_id($id);
        if(!$school) return View('error');
        $data['school']=$school;
        $data['school_admins'] = $this->school_admin_model->get_by_school_id_with_user_info($id);
        $data['major_count'] = $this->major_model->count_by_school_id($id);
        $data['teacher_count'] = $this->teacher_model->count_by_school_id($id);
        $data['logic_class_count'] = $this->logic_class_model->count_by_school_id($id);
        $data['student_count'] = $this->student_model->count_by_school_id($id);
        $this->load->view('admin_school/check',$data);
    }
    function school_admin_add($id=0)
    {
        //表单验证
        if($this->form_validation->run('admin_school/school_admin_add'))
        {
            //post方法
            //读取数据
            $school_id=$this->input->post('school_id');
            $username=$this->input->post('username');
            $name=$this->input->post('name');
            var_dump($school_id);
            //新建用户
            $user = array(
                'username' => $username,
                'password' => md5($username),
                'role_id' => 4,
                 );
            $this->load->model('user_model');
            $this->user_model->add($user);

            $user_id=$this->db->insert_id();

            $school_admin_item = array(
                'user_id' => $user_id,
                'name' => $name,
                'school_id' => $school_id
                 );
            var_dump($school_id);
            $this->load->model('school_admin_model');
            $this->school_admin_model->add($school_admin_item);

            redirect('admin_school/check'.'/'.$school_id);
        }
        //get方法返回add页面
        $data['title']='添加学校';
        $data['school_id']=$id;

        $this->load->view('admin_school/school_admin_add',$data);
    }
    /**
   * 异步jquery验证
   * @return [type] [description]
   */
  function check_username_exist()
  {
    $username=$this->input->post('username');
    $this->load->model('user_model');
    if($this->user_model->username_exist($username))
      echo json_encode(FALSE);
    else 
      echo json_encode(TRUE);
  }
   /**
    * CI的表单回调函数验证
    * @param  [type] $str [description]
    * @return [type]      [description]
    */
   function username_check($username)
   {
      $this->load->model('user_model');
      if($this->user_model->username_exist($username))
         return FALSE;
      else 
         return TRUE;
   }
   //删除一个学校管理员
   function school_admin_delete($id)
   {
       $this->load->model('school_admin_model');
       $this->load->model('user_model');
       $school_admin = $this->school_admin_model->get_by_id($id);
        if(!$school_admin)
            return $this->load->view('error');
        
        if($this->school_admin_model->delete($id)&&$this->user_model->delete($school_admin['user_id']))
            $this->session->set_flashdata('message','操作成功');
        else
            $this->session->set_flashdata('message','存在相关引用，操作失败');

        redirect('admin_school/check'.'/'.$school_admin['school_id']);
   }
}